import sys
import time
from random import randint
import zmq


def main(url=None):
    ctx = zmq.Context.instance()
    publisher = ctx.socket(zmq.PUB)
    if url:
        publisher.bind(url)
    else:
        publisher.bind("tcp://*:5556")
    time.sleep(1)

    for topic_nbr in range(10000):
        publisher.send_multipart([
            b"%03d" % topic_nbr,
            b"Save Roger"
        ])

    while True:
        try:
            time.sleep(1)
            publisher.send_multipart([
                b"%03d" % randint(0, 999),
                b"Off with his head!"
            ])
        except KeyboardInterrupt:
            print("interrupted")
            break


if __name__ == "__main__":
    main(sys.argv[1] if len(sys.argv) > 1 else None)
